#include "admin_common.h"
#include <iostream>
using namespace std;

// 链表节点结构体
struct node
{
    void *data;
    node *prev;
    node *next;
};

// 获取链表头节点
static node *get_head(linked_list *list)
{
    return *(node **)((char *)list + 0);
}

// 获取链表尾节点
static node *get_tail(linked_list *list)
{
    return *(node **)((char *)list + sizeof(void *));
}

// 构造函数
admin::admin() : admin_id(0), admin_name(""), admin_password("") {}
admin::admin(int id, const string &name, const string &password)
    : admin_id(id), admin_name(name), admin_password(password) {}
// 析构函数
admin::~admin() {}
// get方法
int admin::get_admin_id() { return admin_id; }
string admin::get_admin_name() { return admin_name; }
string admin::get_admin_password() { return admin_password; }
// set方法
void admin::set_admin_id(int id) { admin_id = id; }
void admin::set_admin_name(const string &name) { admin_name = name; }
void admin::set_admin_password(const string &password) { admin_password = password; }
// 显示管理员信息
void admin::show_admin_msg()
{
    cout << "ID: " << admin_id << ", 用户名: " << admin_name << ", 密码: " << admin_password << endl;
}

// 链表操作相关函数
// 添加管理员到链表
void add_admin(linked_list *list, admin *adm)
{
    list->insert_tail(list, adm);
}
// 查找管理员
admin *find_admin(linked_list *list, int id)
{
    node *cur = get_head(list);
    while (cur)
    {
        admin *adm = static_cast<admin *>(cur->data);
        if (adm->get_admin_id() == id)
            return adm;
        cur = cur->next;
    }
    return nullptr;
}
// 修改管理员
bool update_admin(linked_list *list, int id, const string &name, const string &password)
{
    node *cur = get_head(list);
    while (cur)
    {
        admin *adm = static_cast<admin *>(cur->data);
        if (adm->get_admin_id() == id)
        {
            adm->set_admin_name(name);
            adm->set_admin_password(password);
            return true;
        }
        cur = cur->next;
    }
    return false;
}
// 删除管理员
bool delete_admin(linked_list *list, int id)
{
    node *cur = get_head(list);
    int pos = 0;
    while (cur)
    {
        admin *adm = static_cast<admin *>(cur->data);
        if (adm->get_admin_id() == id)
        {
            list->delete_middle(list, pos);
            return true;
        }
        cur = cur->next;
        pos++;
    }
    return false;
}
// 显示所有管理员
void print_admins(linked_list *list)
{
    node *cur = get_head(list);
    cout << "\n所有管理员信息：" << endl;
    while (cur)
    {
        admin *adm = static_cast<admin *>(cur->data);
        adm->show_admin_msg();
        cur = cur->next;
    }
}
